package com.mdxx.xczd.ui.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.mdxx.xczd.R;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.bean.UserBean;
import com.mdxx.xczd.ui.base.BaseActivity;
import com.mdxx.xczd.ui.contract.SelectUserContract;
import com.mdxx.xczd.ui.model.SelectUserModel;
import com.mdxx.xczd.ui.presenter.SelectUserPresenter;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by MD-02 on 2017/1/21.
 */

public class SelectUserActivity extends BaseActivity<SelectUserPresenter, SelectUserModel> implements SelectUserContract.View{

    @BindView(R.id.btn_refresh)
    Button mRefresh;
    private ListView listView;
    private ArrayAdapter<String> arr_adapter;
    private List<UserBean> userBeanList;

    @Override
    protected int getLayout() {
        return R.layout.activity_select_user;
    }

    @Override
    protected void init() {
        listView = (ListView)findViewById(R.id.login_list_user);
        listView.setOnItemClickListener(onItemClick);
        listView.setSelection(0);
        mPresenter.getUserRequest();
    }

    @Override
    public void initPresenter() {
            mPresenter.setVM(this, mModel);
    }

    @Override
    public void returnUserData(List<UserBean> userBeanList) {
        List<String> data_list = new ArrayList<String>();
        this.userBeanList = userBeanList;
        for (int i = 0; i < userBeanList.size(); i++) {
            data_list.add(userBeanList.get(i).empNum+" "+userBeanList.get(i).empName);
        }
        arr_adapter= new ArrayAdapter<String>(this,R.layout.list_user_item, data_list);
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        listView.setAdapter(arr_adapter);
    }

    @Override
    public void returnLoginData() {
        startActivity(TaskActivity.class);
        finish();
    }

    @Override
    public void showLoading(String title) {}

    @Override
    public void stopLoading() {}

    @Override
    public void showErrorTip(String msg) {}

    @OnClick(R.id.btn_refresh)
    void onClick() {
        mPresenter.getUserRequest();
    }

    private AdapterView.OnItemClickListener onItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            String userId = userBeanList.get(arg2).empNum;
            String password = userBeanList.get(arg2).empPwd;
            App.USERID = userId;
            mPresenter.getLoginRequest(userId, password);
        }
    };
}

